import time
import logging

logger_statistics = logging.getLogger('statistics')


class TestMiddle():
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        print('before call')
        print(request)
        response = self.get_response(request)
        print('after call')
        return response


class StatisticsMiddle():
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        print('before call')
        start_time = time.time()
        print(request)
        path = request.path
        # full_path=request.get_full_path() 同request.path
        response = self.get_response(request)

        end_time = time.time()
        print('after call')
        # 将时间戳变成人类易读的方式
        log_dict = {
            'start_time:': start_time,
            'used_time': end_time - start_time,
            'path': path
        }
        logger_statistics.info(repr(log_dict))
        return response
